<template>
  <div class="item-one" ref="itemOne">
    <div class="title">
        <span>标签管理</span>
        <div>
            <el-button type="primary" @click="eqma = true">新增标签</el-button>
        </div>
    </div>
    <div class="container">
      <div class="screeningBox">
        <div class="leftBox">
          <!-- <div class="leftitem">
            <el-input class="merchants" v-model="label_name" placeholder="标签名称"></el-input>
          </div> -->
          <div class="leftitem" v-if="userinfo.rolesId == 1 || userinfo.rolesId == 2 || userinfo.rolesId == 8 || userinfo.rolesId == 9">
            <el-form label-width="80px">
              <el-select class="servicename1" v-model="dimension"  placeholder="选择维度" @change="dimension_menu">
                <el-option keys="1" label="超级管理员" :value="1" v-if="userinfo.rolesId == 1"></el-option>
                <el-option keys="2" label="运营" :value="8" v-if="userinfo.rolesId == 1 || userinfo.rolesId == 8 || userinfo.rolesId == 9"></el-option>
                <el-option keys="2" label="财务" :value="9" v-if="userinfo.rolesId == 1 || userinfo.rolesId == 8 || userinfo.rolesId == 9"></el-option>
                <el-option keys="2" label="一级服务商" :value="2" v-if="userinfo.rolesId == 1 || userinfo.rolesId == 2"></el-option>
                <!-- <el-option keys="2" label="门店" :value="6"></el-option> -->
              </el-select>
              <el-autocomplete class="merchants" v-model="tagname" :fetch-suggestions="querySearch_tag" @select="serviceid_tag" placeholder="标签名称"></el-autocomplete>
            </el-form>
          </div>
          <div class="leftitem">
          </div>
        </div>
        <div class="rightBox">
          
          <div class="rightitem">
              <el-button icon="el-icon-search" type="primary" @click="soso" :disabled="isDisable">搜索</el-button>
              <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
          </div>
        </div>
      </div>

      <div class="tableBox">
        <el-table
          ref="multipleTable"
          tooltip-effect="dark"
          style="width: 100%"
          :data="tableData"
          :header-cell-style="tableHeaderColor"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column align="center" type="index" label="序号"></el-table-column>
          <el-table-column prop="name" align="center" label="标签名称"></el-table-column>
          <el-table-column prop="createTime" align="center" label="创建时间"></el-table-column>
          <el-table-column prop="creatorRoleName" align="center" label="创建人角色"></el-table-column>
          <el-table-column align="center" width="500" label="标签描述">
            <template slot-scope="scope">
              <p class="table_p">{{scope.row.description}}</p>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="120">
            <template slot-scope="scope">
              <el-button type="text" @click="jupminfo(scope.row)">详情</el-button>
              <el-button type="text" @click="updateTag(scope.row)" v-if="userinfo.rolesId == 1 || scope.row.creatorRole == userinfo.rolesId">编辑</el-button>
              <el-button type="text" @click="deleteTag(scope.row.id)" v-if="userinfo.rolesId == 1 || scope.row.creatorRole == userinfo.rolesId">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="sizechange"
            :page-sizes="[5,10,20,30,40,50]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :current-page.sync="cur_page"
          ></el-pagination>
        </div>
      </div>
      <br>
      <br>
      <!-- 新增标签 -->
      <el-dialog v-dialogDrag title="新增标签" :visible.sync="eqma" width="35%" :close-on-click-modal="false">
        <el-form
          status-icon
          ref="facility"
          label-width="110px"
          class="demo-dynamic wid100"
        >
          <el-form-item label="标签名称：" required>
            <el-input type="text" placeholder="限10字以内" autocomplete="off" v-model="add_tagname" maxlength="10"></el-input>
          </el-form-item>
          <el-form-item label="标签描述：" required>
            <el-input type="textarea" placeholder="限60字以内" v-model="add_tagdesc" maxlength="60"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="eqma = false">取消</el-button>
          <el-button type="primary" @click="addTag">确定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑标签 -->
      <el-dialog v-dialogDrag title="编辑标签" :visible.sync="visible" width="35%" :close-on-click-modal="false">
        <el-form
          status-icon
          ref="facility"
          label-width="110px"
          class="demo-dynamic wid100"
        >
          <el-form-item label="标签名称：" required>
            <el-input type="text" placeholder="限10字以内" autocomplete="off" v-model="up_tagname" maxlength="10"></el-input>
          </el-form-item>
          <el-form-item label="标签描述：" required>
            <el-input type="textarea" placeholder="限60字以内" v-model="up_tagdesc" maxlength="60"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="update_Tag">确定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getlabelTagPage, getlabelTagSave, getlabelTagUpdate, getlabelTagDelete, getlabelTagList } from "@/api/api";
export default {
  name: "item-one",
  data: function() {
    return {
      loading: false,
      isDisable: false,
      tableData: [],
      cur_page: 1,
      cur_size: 10,
      total: 0,
      eqma: false,//新增弹窗控制
      label_name:'',//标签名称
      add_tagname:'',//新增标签 标签名
      add_tagdesc:'',//新增标签 标签描述
      visible: false,//编辑弹框控制
      up_tagId:'',//编辑标签id
      up_tagname:'',//编辑标签名称
      up_tagdesc:'',//编辑标签描述
      userinfo:'',
      dimension:'',//选择维度
      tagname:'',//标签名
      label_all:[],//标签模糊查询
      tagId:''
    };
  },
  created() {
    this.userinfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.getlabelTagPage()
  },
  methods: {
    //修改thead 背景色
      tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;'
      }
    },
    // 刷新
    // refresh() {
    //   this.getInventory()
    // },
    // 分页导航
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getlabelTagPage()
    },
    // 分页数量
    sizechange(val) {
      this.cur_size = val;
      this.getlabelTagPage()
    },
    //多选框
    // handleSelectionChange(val) {
    //   this.multipleSelection = val;
    // },
    //  重置
    reset() {
      this.label_name = ''
      this.dimension = ''
      this.tagname = ''
      this.tagId = ''
    },
    // 搜索
    soso() {
      this.cur_page = 1
      this.getlabelTagPage()
    },
    //获取标签列表数据
    getlabelTagPage(){
      this.loading = true
      this.isDisable = true
      getlabelTagPage({
        name: this.label_name,
        page: this.cur_page,
        size: this.cur_size,
        id: this.tagId
      }).then(res => {
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if(res.code == 200){
          this.isDisable = false
          this.loading = false
          this.tableData = res.data
          this.total = res.pageInfo.recordCount
          // this.profit = res.data.profit
          // this.total1 = res.pageInfo.recordCount;
        }else if(res.code == 500){
          this.isDisable = false
          this.loading = false
          this.$message.error(res.message);
        }
      })
    },
    //新增标签
    addTag(){
      getlabelTagSave({
        name: this.add_tagname,
        description: this.add_tagdesc
      }).then(res => {
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if(res.code == 200){
          this.add_tagname = ''
          this.add_tagdesc = ''
          this.eqma = false
          this.getlabelTagPage()
        }else if(res.code == 500){
          this.$message.error(res.message);
        }
      })
    },
    //编辑标签
    updateTag(v){
      console.log(v)
      this.visible = true
      this.up_tagId = v.id
      this.up_tagname = v.name
      this.up_tagdesc = v.description
    },
    //编辑提交
    update_Tag(){
      getlabelTagUpdate({
        name: this.up_tagname,
        description: this.up_tagdesc
      },this.up_tagId).then(res => {
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if(res.code == 200){
          this.visible = false
          this.getlabelTagPage()
        }else if(res.code == 500){
          this.$message.error(res.message);
        }
      })
    },
    //删除标签
    deleteTag(v){
      this.$confirm(`是否确定删除该标签？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(() => {
        getlabelTagDelete({},v).then(res => {
          if (res.code == 405) {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
          } else if(res.code == 200){
            this.$message({
                  message: '删除成功',
                  type: 'success'
                });
            this.getlabelTagPage()
          }else if(res.code == 500){
            this.$message.error(res.message);
          }
        })
      })
      .catch(() => {
          this.$message({
              type: "info",
              message: "已取消"
          });
      });
    },
    // 详情
    jupminfo(val){
      console.log(val)
      this.$router.push({
          path: "/labelDetails"
      });
      sessionStorage.setItem('labelDetails_id',JSON.stringify(val.id))
    },
    //维度查询标签
    dimension_menu(v){
      this.label_all = []
      getlabelTagList({
        creatorRole: v
      }).then(res => {
        if(res.code == 405){
          sessionStorage. removeItem('token')
          this.$router.push('/login');
        }else if(res.code == 200){
          for(var i=0;i<res.data.length;i++){
            this.label_all.push({'value':res.data[i].name,'id':res.data[i].id})
          }
        }else{
          this.$message.error(res.message);
        }
      })
    },
    //标签模糊
    querySearch_tag(queryString,cb){
      let servicearr = this.label_all;
      let results = queryString
        ? servicearr.filter(this.createFilter(queryString))
        : servicearr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    serviceid_tag(item){
      console.log(item)
      this.tagId = item.id
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !==
          -1
        );
      };
    },
  }
};
</script>
<style scoped lang='less'>
.table_p{
    text-indent: 24px;
    text-align: center;
    letter-spacing:2px;
}
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 50%;
  padding: 0 15px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.tableBox {
  margin-top: 20px;
}
.red {
  color: #f9607d;
}
.blue {
  color: #01aaef;
}
.stitle {
  padding: 5px 0;
  font-size: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  i {
    margin-left: 10px;
    margin-right: 5px;
  }
  .swarning {
    color: red;
  }
  .warning {
    color: #f9607d;
  }
}
.merchants{
  width: 50%;
}
</style>